<template>
  <view>
    <u-popup :show="show" mode="bottom" @close="closeShow" :round="23">
      <div style="height:80vh;overflow-y: auto;">
        <div class="popup-con">
          <u-sticky bgColor="#fff" offset-top="1" :customNavHeight="0">
			  <div class="popupTitle" style="text-align: left;padding-left:16px;">
			    <img src="https://pic.moresu.com/FtL6uPkrQHkp6_svJiopVvp0zAf4"
			  	style="width:14px;height:14px;display: inline-block;vertical-align: middle;" />
			    <span style="padding-left:20rpx;vertical-align: middle;">选择品名</span>
			  </div>
            <div style="padding:10rpx 0">
              <u-search placeholder="输入品名" @search="searchGetList1" v-model="keywords" shape="square" bgColor="#fff"
                :showAction="false" borderColor="#DEDEDE"></u-search>
              <div class="category-tab">
                <u-tabs :current="current" :list="list" @click="clickTab" lineColor="#FF5E01"
                  :activeStyle="{color: '#FF5E01', fontWeight: 'bold',fontSize:'14px'}"
                  :inactiveStyle="{color: '#929292',fontSize:'14px'}"></u-tabs>
              </div>
            </div>
          </u-sticky>

          <div class="category-list">
            <div v-if="list1.length > 0">
              <div class="list-item" v-for="(item,index) in list1" :key="index">
                <div class="item-title">{{item.title}}</div>
                <div class="item-list">
                  <div class="item-ename" :class="{'choose': type == 'more' && chooseList.indexOf(ee.categoryId)!= -1}"
                    v-for="(ee,ii) in item.list" :key="ii" @click="chooseItem(ee)">{{ee.name}}</div>
                </div>
              </div>
            </div>
            <u-empty text="暂无数据" mode="list" v-else-if="list1.length == 0 && !listLoading"
              icon="https://pic.moresu.com/Fs-rZPgApAIGT8szof2GUATIiF8N"></u-empty>
          </div>
        </div>
  
      </div>
	  <div class="zdy-div" v-if="type != 'more'">
		  <div class="zdy-btn" v-if="!show2" @click="show2=true">自定义</div>
		  <div class="zdy-form" v-else>
			  <div style="padding-right:10rpx;flex:1">
				   <u--input placeholder="请输入自定义品名"  border="surround"  v-model="value" size="small"></u--input>
			  </div>
			 
			  <div style="width:120rpx;">
				  <u-button text="确认" shape="circle" type="primary" size="small" @click="savezdy"></u-button>
			  </div>
			  <div style="width:10rpx"></div>
			  <div style="width:120rpx">
				  <u-button text="取消" shape="circle" type="primary" size="small" plain @click="show2=false;value=''"></u-button>
			  </div>
		  </div>
	  </div>
      <div style="width:100%;padding:24rpx;bottom:0;background: #fff;border-top:1px solid #eee" v-if="type == 'more'">
        <u-button text="确认" shape="circle" type="primary" @click="save"></u-button>
      </div>

    </u-popup>
  </view>
</template>
<script>
  import {
    imgCheck
  } from '@/plugins/upload/upload.js'
  import {
    getClassList,
    getSubClassList,
	storeTypeList
  } from '@/config/api.js'
  import store from '@/store';
  const log = require('@/plugins/log.js')
  export default {
    name: 'category',
    computed: {


    },
    data() {
      return {
        show: false,
        keywords: '',
        list: [],
        list1: [],
        classId: '',
        current: 0,
        listLoading: false,
        type: '',
        chooseList: [],
        chooseFullList: [],
		show2:false,
		value:'',
		currentRoute:'',
		storeType:'',
		typeList:{}
      }
    },
    watch: {

    },
    created() {


    },
    methods: {
      init(type, list,storeType) {
        this.keywords = ''
        this.list = []
        this.list1 = []
        this.current = 0
        this.show = true;
		this.value = ''
        console.log(this.current)
        this.getList();
        this.type = type ? type : 'one';
        this.chooseList = [];
        this.chooseFullList = [];
		this.getTypeList();
		this.storeType = storeType ? storeType : '';
		console.log(list)
        if (list) {
          list.forEach((el) => {
            this.chooseList.push(el.categoryId);
            console.log('this.chooseList', this.chooseList)
            this.chooseFullList.push(el)
          })
        }
		var pages = getCurrentPages() // 获取栈实例
		this.currentRoute = pages[pages.length - 1].route; // 获取当前页面路由
		console.log("路由当前页面路径"+this.currentRoute)
      },
	  
	  getTypeList(){
		  storeTypeList().then((res)=>{
			  this.typeList = res
		  })
	  },

      searchGetList1() {
		if(this.currentRoute.indexOf('index/index') != -1){
			log.info('精准询价&品名弹窗搜索框&' + this.keywords)
		}  
        this.getList1()
      },

      getList() {
        getClassList().then((res) => {
          this.list = res;
          this.classId = this.list[this.current] ? this.list[this.current]['categoryId'] : '';
          this.getList1()
        })
      },
      getList1() {
        this.listLoading = true;
        getSubClassList({
          classId: this.classId,
          keywords: this.keywords
        }).then((res) => {
          this.list1 = res;
          this.listLoading = false
        }).catch(() => {
          this.listLoading = false
        })
      },
      clickTab(item) {
        this.classId = item.categoryId
        this.current = item.index;
        this.getList1()
		if(this.currentRoute.indexOf('index/index') != -1){
			log.info('精准询价&品名弹窗顶部大类&' + item.name)
		}else if(this.currentRoute.indexOf('inquiry/inquiryList') != -1){
			log.info('品名筛选&类别')
		} 
        
      },
	  
	  savezdy(){
		  let firstName = this.list[this.current]['name']; 
		  if(this.value){
			 this.$emit('chooseItem', {categoryId:0,name:this.value},firstName,this.classId)  
		  }else{
			  uni.showToast({
			    title: '请输入自定义品名',
			    icon: 'none'
			  });
			  return false
		  }
		 
		  this.show = false  
	  },

      chooseItem(e) {
	    let firstName = this.list[this.current]['name'];
        if (this.type != 'more') {
			if(this.currentRoute.indexOf('index/index') != -1){
				log.info('精准询价&品名弹窗品名&' + e.name)
			}else if(this.currentRoute.indexOf('inquiry/inquiryList') != -1){
				log.info('品名筛选&品类')
			}  
          this.$emit('chooseItem', e,firstName,this.classId)
          this.show = false
        } else {
          
			  let i = this.chooseList.findIndex((el) => {
			    return el == e.categoryId
			  })
			  if (i != -1) {
			    console.log(i)
			    this.chooseList.splice(i, 1);
			    this.chooseFullList.splice(i, 1)
			  } else {
				  let num = 99
				  if(this.storeType != ''){
					  num = this.typeList[this.storeType]['goodsNum']
				  }
				  if(this.chooseList.length < num ){
					  console.log(firstName)
					  this.chooseList.push(e.categoryId);
					  this.chooseFullList.push({
					    categoryId: e.categoryId,
					    categoryName: e.name,
					    firstName:firstName
					  });
					console.log("chooseFullList",this.chooseFullList)
				  }else{
					  let txt = '最多只能选'+num+'项'
					  uni.showToast({
					    title: txt,
					    icon: 'none'
					  });
				  }
			  			 
			  }
        }

      },
      closeShow() {
        this.show = false
      },

      save() {
		  let num = 99
		  if(this.storeType != ''){
		  num = this.typeList[this.storeType]['goodsNum']
		  }
		  if(this.chooseList.length <= num ){
				console.log("this.chooseFullList",this.chooseFullList)
				this.$emit('chooseItem', this.chooseFullList)
				this.show = false		 
		  }else{
			  let txt = '最多只能选'+num+'项'
			  uni.showToast({
				title: txt,
				icon: 'none'
			  });
		  }
		  
	   
      },

    }
  };
</script>

<style lang="scss" scoped>
  @import '@/style/common.scss';

  .popupTitle {
    font-size: $b-font;
    font-weight: bold;
    text-align: center;
    line-height: 50px;
  }

  .popup-con {
    padding: 0 16px 16px 16px;

    .popup-block-pay {
      text-align: center;
      padding: 20px;

      .price-sub-title {
        font-size: $s-font;
        color: $g-font-color;
        line-height: 30px;
      }

      .price-big-title {
        font-size: 22px;
        color: $theme-color;
        font-weight: bold;
        margin-top: 10px;
      }
    }

    .popup-label {
      line-height: 40px;
      font-size: $m-font;
      color: $g-font-color;
      @include flexbox(space-between, center)
    }

    .img-div {
      width: 210px;
      height: 210px;
      border: 1px solid $border-color;
      margin: 0 auto;
      background: #f4f5f7;
    }

    .send-list {
      padding-top: $marginSpace;

      .send-item {
        border-radius: 8px;
        padding: 12px;
        box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
        margin-bottom: $marginSpace;

        .send-top {
          @include flexbox(flex-start, flex-start);
          font-size: $m-font;

          .name {
            padding-right: 12px;
            flex: 1
          }

          .num {
            font-weight: bold;
            color: $theme-color
          }
        }

        .send-middle {
          font-size: $s-font;
          color: $g-font-color;
          line-height: 18px;
          margin-top: 5px;
        }

        .send-bottom {
          text-align: right;
          font-size: $s-font;
          margin-top: 16px
        }
      }
    }

    .category-list {
      padding: 36rpx 0;

      .list-item {
        @include flexbox(flex-start, flex-start);
        margin-bottom: 20rpx;

        .item-title {
          width: 126rpx;
          height: 52rpx;
          background: rgba(255, 255, 255, 0.39);
          border: 1px solid #B8B8B8;
          border-radius: 9rpx;
          font-size: $m-font;
          text-align: center;
          line-height: 52rpx;
        }

        .item-list {
          padding-left: 30rpx;
          width: calc(100% - 126rpx);
          overflow: hidden;

          .item-ename {
            font-size: $s-font;
            color: #000;
            line-height: 52rpx;
            width: 170rpx;
            @include textoverflow(1);
            float: left;
          }

          .item-ename.choose {
            color: #FF5E01;
            font-weight: bold;
          }
        }
      }
    }

    .goodsSn-list {
      border: 1px solid #DEDEDE;
      padding: 0 10px;
      font-size: $s-font;
      margin-top: 15px;
      padding-bottom: 10px;

      .list-cell {
        border-bottom: 1px solid #DEDEDE;
        @include flexbox(space-between, center);
        line-height: 60rpx;
        height: 60rpx;
      }

      .list-cell:first-child {}
    }

    .audio-text {
      font-size: 34rpx;
      line-height: 48rpx;
      color: #414141;
      text-align: center;
      padding-top: 100rpx;
    }

    .audio-dh {
      @include flexbox(space-between, center);
    }
  
  }
  .zdy-div{
	  width:100%;
	  padding:24rpx;
	  bottom:0;
	  background: #fff;
	  height:128rpx;
	  .zdy-btn{
		  font-size:28rpx;
		  color:#FF5E01;
		  line-height: 80rpx;
	  }
  }
  .zdy-form{
	 @include flexbox(space-between, center)
  }

  .popup-bottom {
    background: #fff;
    margin-top: 10px;
    padding: 16px;
  }

  .popup-bottom-fixed {
    position: fixed;
    width: 100%;
    padding: 16px;
    box-sizing: border-box;
    bottom: 0
  }
</style>